package com.lovo.ssc.controller;


import com.lovo.ssc.bean.SysUser;
import com.lovo.ssc.service.ILevelService;
import com.lovo.util.*;
import com.lovo.ssc.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户控制层张小龙")

public class SysUserController {
    @Autowired
    ISysUserService sysUserService;

    @Autowired
    ILevelService service;

    @ApiOperation(value = "用户登陆返回(tom,123)，成功返回token，失败1-用户冻结，2-用户未注册，3-用户或密码错误")
    @GetMapping("login/{userName}/{pwd}")
    public String login(@PathVariable("userName") String userName,@PathVariable("pwd") String pwd,HttpServletRequest request){
        SysUser user=sysUserService.login(userName, pwd);
        //返回1-用户冻结，2-用户未注册，3-用户或密码错误
        if (user!=null){
            if (user.getUserFreeze()==0){
                user.setUserDiscount(service.findDiscount(user.getUserLevel()));
                request.getSession().setAttribute("loginUser",user);
                return CreateToken.createToken(userName);
            }else {
                return "1";
            }
        }else if (sysUserService.findByUserName(userName) == null){
            return "2";
        }else {
            return "3";
        }
    }

    @ApiOperation(value = "根据token取出用户名，查询用户对象")
        @GetMapping("findByUser")
    public SysUser findByUser(HttpServletRequest request){
        String token=request.getHeader("token");
        String userName=Verifier.verifierJwt(token);
        return sysUserService.findByUserName(userName);
    }

    @ApiOperation(value = "根据用户名查询用户")
    @GetMapping("findByUserName")
    public String findByUserName(String userName){
        SysUser user=sysUserService.findByUserName(userName);
        if (user!=null){
            return "ok";
        }
        return "no";
    }

    @ApiOperation(value = "设置用户审核状态远程")
    @GetMapping("setAuditStatus/{userName}/{code}/{reason}/{level}")
    public String setAuditStatus(@PathVariable("userName") String userName,@PathVariable("code")Integer code,@PathVariable("reason")String reason,@PathVariable("level")String level){

        return sysUserService.setAuditStatus(userName, code, reason, level);
    }

    @ApiOperation(value = "设置用户冻结状态远程")
    @GetMapping("setFreeze/{userName}/{code}")
    public String setFreeze(@PathVariable("userName")String userName,@PathVariable("code")Integer code){
        return sysUserService.setFreeze(userName, code);
    }

}

